using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Sedna.API.Remoting;
using Sedna.Core.Controller;
using Sedna.Utility.Command;
using Sedna.Utility.Command.Registry;
using log4net.Config;

namespace Sedna.Utility.Core
{
    public class Application
	{

		#region Constants

		private const int SUCCESS_STATUS = 0;
        private const int ERROR_STATUS = 1;

		#endregion

		#region Entry Point

		/// <summary>
        /// Defines entry point for the application.
        /// </summary>
        static int Main(string[] args)
        {
            XmlConfigurator.Configure();
            try
            {
                if (Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["trustAnyCertificate"]))
                    System.Net.ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(IsCertificateValid);

                RemotingConfigurator.Configure(Assembly.GetExecutingAssembly());

                ICommandRegistry registry =
                    (ICommandRegistry)SpringContextFactory.GetContext().GetObject("command.Registry");
                if (args.Length > 0)
                {
                    List<string> parameters = new List<string>();
                    foreach (string param in args)
                    {
                        if (param.Equals(args[0]))
                        {
                            continue;
                        }
                        parameters.Add(param);
                    }
                    CommandResult result = registry.ExecuteCommand(args[0], parameters.ToArray());
                }
                else
                {
                    registry.DisplayHelp();                    
                }

                return SUCCESS_STATUS;
            }
            catch (Exception)
            {
                return ERROR_STATUS;
            }
        }

        #endregion

		#region Helpers

		private static bool IsCertificateValid(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
		}

		#endregion

	}
}
